﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Data.SqlClient;

namespace QLBenhVien
{
    class DAOHoaDon
    {
        public static string table_name = "HoaDon";
        public static void Add(DBOHoaDon o)
        {
            string sql = "select * from " + table_name;
            DataTable dt = DB_Provider.ReadSchema(sql);
            DataRow dr = dt.NewRow();
            foreach (PropertyDescriptor pro in TypeDescriptor.GetProperties(o))
            {
                dr[pro.Name] = pro.GetValue(o);
            }
            dt.Rows.Add(dr);
            DB_Provider.Write(dt, table_name);
        }

        public static DataTable SearchByID(int id)
        {
            string sql = "select * from hoadon join hoadonthuoc on hoadonthuoc.hoadon_id = hoadon.id join hosobenhnhan on hosobenhnhan.id = hoadon.benhnhan_id join khothuoc on khothuoc.id = hoadonthuoc.thuoc_id where hoadon.id = @id";
            DataTable dt = new DataTable();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, DB_Provider.conn_str);
                da.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;
                da.Fill(dt);
            }
            catch (Exception ex) { return null; }
            return dt;
        }
    }
}
